package com.literature.search.core.config.esindex;

import com.literature.search.core.common.EsIndexConstant;
import com.literature.search.core.mapper.CNEsFileObjectMapper;
import com.literature.search.core.mapper.ENEsFileObjectMapper;
import com.literature.search.core.mapper.HistoryKeywordsMapper;
import com.literature.search.core.utils.SpringApplicationContext;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @Author: DangKong
 * @Date: 2023/3/23 09:01
 * @Description:
 */
@Component
@Order(value = 10)
public class EsIndexApplicationRun implements ApplicationRunner {

    /**
     * @Description: 项目启动后执行:检查索引是否存在，不存在则创建索引
     * @param applicationArguments
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {

        CNEsFileObjectMapper cnEsFileObjectMapper = SpringApplicationContext.getBean(CNEsFileObjectMapper.class);
        ENEsFileObjectMapper enEsFileObjectMapper = SpringApplicationContext.getBean(ENEsFileObjectMapper.class);
        HistoryKeywordsMapper historyKeywordsMapper = SpringApplicationContext.getBean(HistoryKeywordsMapper.class);

        if (!cnEsFileObjectMapper.existsIndex(EsIndexConstant.CN_INDEX_NAME)){
            System.out.println("cn索引不存在，创建索引");
            cnEsFileObjectMapper.createIndex();
        }
        if (!enEsFileObjectMapper.existsIndex(EsIndexConstant.EN_INDEX_NAME)){
            System.out.println("en索引不存在，创建索引");
            enEsFileObjectMapper.createIndex();
        }
        if (!historyKeywordsMapper.existsIndex(EsIndexConstant.HISTORY_KEYWORDS_INDEX_NAME)){
            System.out.println("history_keywords索引不存在，创建索引");
            historyKeywordsMapper.createIndex();
        }
    }


}
